package ${domain}.${moduleName};

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
<#if table.getImport()?length gt 1>
${table.getImport()}
</#if>
import ${domain}.frame.auth.Token;
<#if table.needMgr()>
import ${domain}.module.${moduleName}.mgr.${table.getCName()}Manager;
</#if>
<#if table.getCreate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}CreateRequest;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.req.${table.getCName()}DeleteRequest;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.req.${table.getCName()}FindRequest;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.req.${table.getCName()}GetRequest;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.req.${table.getCName()}SearchRequest;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}UpdateRequest;
</#if>
<#if table.getCreate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}CreateResponse;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}DeleteResponse;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}FindResponse;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}GetResponse;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}SearchResponse;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}UpdateResponse;
</#if>

import static junit.framework.TestCase.assertTrue;

/**
 * ${table.getCName()}Test -  - ${table.tableComment}测试用例
 *
 * @author ${author?default("")}
 * @version 0.0.1
 * @since ${.now?string["yyyy-MM-dd"]}
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ${table.getCName()}Test {

    @Autowired
    private Token token;
<#if table.needMgr()>

    @Autowired
    private ${table.getCName()}Manager ${table.getFName()}Manager;
<#if table.getCreate()>

    @Test
    public void testCreate() {
        ${table.getCName()}CreateRequest request = new ${table.getCName()}CreateRequest();
<#list table.fields as field>
<#if !field.isSystem>
        request.${field.setterName()}(${field.getTestValue()});
</#if>
</#list>

        ${table.getCName()}CreateResponse response = ${table.getFName()}Manager.create(request,token);

        assertTrue(!response.hasError());
    }
</#if>
<#if table.getDelete()>

    @Test
    public void testDelete() {

        //创建数据
        ${table.getCName()}CreateRequest createRequest = new ${table.getCName()}CreateRequest();
<#list table.fields as field>
    <#if !field.isSystem>
        createRequest.${field.setterName()}(${field.getTestValue()});
    </#if>
</#list>

        ${table.getCName()}CreateResponse createResponse = ${table.getFName()}Manager.create(createRequest,token);

<#if table.sys>
        assertTrue(!createResponse.hasError() && createResponse.getId() > 0);
<#else>
        assertTrue(!createResponse.hasError());
</#if>
        //删除数据
        ${table.getCName()}DeleteRequest request = new ${table.getCName()}DeleteRequest();
    <#if table.getSys()>
        request.setId(createResponse.getId());
    <#else>
    </#if>

        ${table.getCName()}DeleteResponse response = ${table.getFName()}Manager.delete(request,token);

        assertTrue(!response.hasError() && response.getResult() == 1L);
    }
</#if>
<#if table.getUpdate()>

    @Test
    public void testUpdate() {
        //创建数据
        ${table.getCName()}CreateRequest createRequest = new ${table.getCName()}CreateRequest();
<#list table.fields as field>
    <#if !field.isSystem>
        createRequest.${field.setterName()}(${field.getTestValue()});
    </#if>
</#list>

        ${table.getCName()}CreateResponse createResponse = ${table.getFName()}Manager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        ${table.getCName()}UpdateRequest request = new ${table.getCName()}UpdateRequest();
<#if table.getSys()>
        request.setId(createResponse.getId());
</#if>
<#list table.fields as field>
    <#if !field.isSystem>
        request.${field.setterName()}(${field.getTestValue()});
    </#if>
</#list>

        ${table.getCName()}UpdateResponse response = ${table.getFName()}Manager.update(request,token);

        assertTrue(!response.hasError() && response.getResult() == 1L);
    }
</#if>
<#if table.getFind()>

    @Test
    public void testFind() {
        ${table.getCName()}FindRequest request = new ${table.getCName()}FindRequest();
<#list table.fields as field>
    <#if !field.isSystem && field.isQuery>
        request.${field.setterName()}(${field.getTestValue()});
    </#if>
</#list>

        ${table.getCName()}FindResponse response = ${table.getFName()}Manager.find(request,token);

        assertTrue(!response.hasError());
    }
</#if>
<#if table.getGet()>

    @Test
    public void testGet() {
        //获得数据
        ${table.getCName()}GetRequest request = new ${table.getCName()}GetRequest();
<#if table.getSys()>
        request.setId(0L);
</#if>

        ${table.getCName()}GetResponse response = ${table.getFName()}Manager.get(request,token);

        assertTrue(!response.hasError() && response.get${table.getCName()}() != null);
    }
</#if>
<#if table.getSearch()>

    @Test
    public void testSearch() {
        ${table.getCName()}SearchRequest request = new ${table.getCName()}SearchRequest();
		request.setKeyword("");

        ${table.getCName()}SearchResponse response = ${table.getFName()}Manager.search(request,token);

        assertTrue(!response.hasError());
    }
</#if>
</#if>
}
